<template>
  <div class="login-page">
    <el-card class="el-card">
      <template #header>黑马面经运营后台</template>
      <el-form :label-position="labelPosition" :model="form" :rules='rules' status-icon ref="form">
        <el-form-item label="用户名：" prop='username'>
          <el-input placeholder="请输入用户名" v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码：" prop='password'>
          <el-input placeholder="请输入密码" type='password' v-model="form.password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

  </div>
</template>

<script>
import { login } from '@/api/user'
export default {
  name: 'LoginIndex',
  data () {
    return {
      labelPosition: 'top',
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: ['blur'] },
          { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: ['blur'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: ['blur'] },
          { pattern: /^\w{5,11}$/, message: '请输入5到10位的密码', trigger: ['blur'] }
        ]
      }
    }
  },
  methods: {
    async login () {
      this.$refs.form.validate(async valid => {
        // console.log(valid)
        if (valid) {
          const res = await login(this.form)
          console.log(res)
          this.$store.commit('user/setUserToken', res.data.token)
          this.$router.push('/')
          this.$message({
            showClose: true,
            message: '恭喜你，登录成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '警告哦，登录失败',
            type: 'warning'
          })
          return false
        }
      })
    },
    reset () {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang='scss' scoped>
.login-page{
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header{
      height: 80px;
      background-color: rgba(114,124,245,1);
      text-align: center;
      color: #fff;
      line-height: 40px;
      font-size: 18px;
    }
  }
}
</style>
